<?php
/**
 * 21点  app 模块
 * 
 * @version $Id$
 */

class Module_21
{
	/**
	 * 构造函数
	 */
	function __construct() {
		
	}

	/**
	 * 21点首页
	 */
	function index() {

		$uin = sessionInfo('uin');
		
		$smarty = loadSmarty();
		
		
		loadLib('App_21');
		loadLib('Currency');
		$stake = Currency::get('app_21' , $uin);		
		// 获取自己的战绩
		$gains = App_21::getGain($uin , false);
		$newbie = false;
		if(empty($gains)) {
			$gains = App_21::getGain($uin , true);
			$newbie = true;
		}
		
		// 获取盈利
		$rank = App_21::getRank($uin , $gains['gain']);
		$smarty->assign('stake' , $stake);
		$smarty->assign('gains' , $gains);
		$smarty->assign('rank' , $rank);
		
		
		// 获取是否存在该局
		$result = App_21::getRandStateList();
		//print_r($result);
		$list = array();
		foreach($result as $k => $row) {
			if($row['uin'] == $uin) continue;	// 不显示自己的
			
			$list[$k] = $row;
			$list[$k]['cards_str'] = App_21::num2card($row['state']['owner_cards']);
			$list[$k]['cards_sum'] = App_21::sum($row['state']['owner_cards']);
		}

		$uin_list = array();
		foreach($list as $row) {
			$uin_list[] = $row['uin'];
			$gains[$row['uin']] = $row['gain'];
		}
		loadLib('User');
		
		//调用战报
		loadLib('App_battlefie');
		$gains_list=App_battlefie::getBattlefie();

		$smarty->assign('gains_list' , $gains_list);
		
		// 获取用户等级信息
		$gains_info = App_21::getRanks($uin_list , $gains);
		if(!empty($uin_list)) {
			$uin_info = User::getBaseInfo($uin_list);
			
			foreach($list as $k => $row) {
				$list_uin = $row['uin'];
				$list[$k]['uin_name'] = isset($uin_info[$row['uin']]) ? $uin_info[$row['uin']]['name'] : '';
				$list[$k]['uin_sex'] = isset($uin_info[$row['uin']]) ? $uin_info[$row['uin']]['sex'] : '';
				$list[$k]['rank'] = $gains_info[$list_uin];
			}
		}
		
		$smarty->assign('list' , $list);
	//	print_r($list);
		// 如果是第一次来，则奖励10000筹码
		if($newbie) {
			loadLib('Currency');
			Currency::update('app_21' , $uin , 10000);
		}
		
		$smarty->assign('newbie' , $newbie);

		return $smarty->fetch('app/21/index.tpl.php');
	}
	
	
	/**
	 * 新开一局
	 */
	function open() {
		$uin = sessionInfo('uin');
		
		$smarty = loadSmarty();
		
		loadLib('Currency');
		$money = Currency::get('app_21' , $uin);
		
		$smarty->assign('money' , $money);
		
		// 判断钱是否够，不够就提示去用筹码换，或者退出
		if($money < 50) {			
			return View::error('一次最少下注50筹码，你现在只有' . $money . '筹码');
		}
		
		// 随机发两张牌
		loadLib('App_21');
		$cards = App_21::deal(2);
		$cards_str = App_21::num2card($cards);
		$cards_sum = App_21::sum($cards);
		
		$smarty->assign('cards' , $cards);
		$smarty->assign('cards_str' , $cards_str);
		$smarty->assign('cards_sum' , $cards_sum);
		
		// 牌是好的，则记录当前状态
		$id = 0;
		$code = App_21::recordState($uin , $id , 0 , 0 , $cards , array() , 50);
		if($code !== 0) {
			return View::error('系统繁忙，请稍后再试' , $code);
		}
		
		
		// 最后一步，扣钱 (就算这步失败了，也是用户赚点，用户不会亏，不会投诉)
		$code = Currency::update('app_21' , $uin , -50);
		if($code !== 0) {
			return View::error('系统繁忙，请稍后再试' , $code);
		}
		loadLib('Operate');
		Operate::getAdd($uin , 0 , 'app_21');
		// 全部成功后，302 跳转到再摸一张的页面，防止用户刷新页面，再次开一局
		header('Location: ' . SITEURL . '/app.php?m=21&a=addcard&uin=' . sessionInfo('uin') . '&id=' . $id . '&sid=' . sessionInfo('skey') );
	}
	
	/**
	 * 加牌（再摸一张）
	 */
	function addcard() {
		$owner_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$add_one = isset($_POST['add']) && $_POST['add'] == 1 ? true : false;
		
		// 异常判断
		if(empty($owner_uin) || $id <= 0) {
			return View::error('系统繁忙，请稍后再试');
		}
		
		$uin = sessionInfo('uin');
		
		// 获取是否存在该局
		loadLib('App_21');
		$state = App_21::getState($owner_uin , $id);
		
		if(empty($state)) {
			return View::error('该局已结束');
		}
		
		$is_own = $uin == $owner_uin ? true : false;
		
		// 判断是否有权限继续摸牌
		if(!$is_own && $state['guest_uin'] > 0 && $state['guest_uin'] != $uin) {
			return View::error('该局已结束');		// 当前用户，不是这幅牌的主人，也不是挑战者
		}
		
		// 判断当前状态，主人是否还有权限继续摸牌
		// ..todo
		
		$smarty = loadSmarty();
		
		$owner_cards = $state['state']['owner_cards'];
		$guest_cards = $state['state']['guest_cards'];
		$stake = $state['stake'];
		
		
		// 判断是否要摸一张牌
		if($add_one) {
			$add_card = App_21::deal(1);
			
			if($is_own) {
				// 主人摸牌
				$owner_cards[] = $add_card;
				$state['state']['owner_cards'] = $owner_cards;
			} else {
				// 挑战者摸牌
				$guest_cards[] = $add_card;
				$state['state']['guest_cards'] = $guest_cards;
			}
		}
		
		// 处理当前牌局状态
		if($is_own) {
			$cards_str = App_21::num2card($owner_cards);
			$cards_sum = App_21::sum($owner_cards);
			
			$smarty->assign('cards' , $owner_cards);
		} else {
			$cards_str = App_21::num2card($guest_cards);
			$cards_sum = App_21::sum($guest_cards);
			
			$smarty->assign('cards' , $guest_cards);
		}
		$smarty->assign('cards_str' , $cards_str);
		$smarty->assign('cards_sum' , $cards_sum);
		
		
		$smarty->assign('is_own' , $is_own);
		$smarty->assign('owner_uin' , $owner_uin);
		$smarty->assign('id' , $id);
		
		// 判断是否爆了
		if($cards_sum > 21) {
			App_21::lose($owner_uin , $id , $uin , $state);
			
			// 如果是客人爆了，则发送系统消息通知主人（主人自己爆了，不需要发系统消息）
			if(!$is_own) {
				loadLib('SysMsg');
				SysMsg::sendSystem($owner_uin , array(
					'sum' => App_21::sum($owner_cards),
					'send_sum' => App_21::sum($guest_cards),
					'send_uin' => $uin,
					'win' => 1,
					'earn' => $stake,
				) , 1001);
			}
		
			$smarty->assign('lose_money' , $stake);
			
			return $smarty->fetch('app/21/overflow.tpl.php');
		}
		
		// 记录当前牌局
		$code = App_21::recordState($owner_uin , $id , $state['guest_uin'] , $state['turn'] , $owner_cards , $guest_cards , $stake);
		if($code !== 0) {
			return View::error('系统繁忙，请稍后再试' , $code);
		}
		/***********************道具列表**********************/
		loadLib('App_prop');
		$oneselfprop = App_prop::getAllOneselfProp('app_21_oneself_prop', 'app_21_prop', $uin);
		foreach ($oneselfprop as $k => $row){
			if ($row['type'] == 'repeat'){
				$list[] = $row;
			}
		}
		
		if (!empty($list)){
			$smarty->assign('list',$list);
		}
		
		return $smarty->fetch('app/21/addcard.tpl.php');
	}
	
	
	/**
	 * 结束牌局，不再摸牌
	 */
	function end() {
		$owner_uin = isset($_POST['uin']) ? intval($_POST['uin']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
		//print_r($owner_uin);
		
		// 异常判断
		if(empty($owner_uin) || $id <= 0) {
			return View::error('系统繁忙，请稍后再试');
		}
		$uin = sessionInfo('uin');
		//查询筹码余额
		loadLib('Currency');
		$balance = Currency::get('app_21' , $uin);	
		// 获取是否存在该局
		loadLib('App_21');
		$state = App_21::getState($owner_uin , $id);
		
		if(empty($state)) {
			return View::error('该局已结束');
		}
		
		$is_own = $uin == $owner_uin ? true : false;
		
		$stake = $state['stake'];
		
		$smarty = loadSmarty();
		$smarty->assign('balance' , $balance);
		$smarty->assign('stake' , $stake);
		$smarty->assign('is_own' , $is_own);
		$smarty->assign('owner_uin' , $owner_uin);
		$smarty->assign('id' , $id);
			
		// 如果是主人自己，则可以加注
		if($is_own) {
			return $smarty->fetch('app/21/addstake.tpl.php');
		}
		
		
		// 判断胜负
		$owner_cards = $state['state']['owner_cards'];
		$guest_cards = $state['state']['guest_cards'];
		$stake = $state['stake'];
		
		$owner_cards_str = App_21::num2card($owner_cards);
		$owner_cards_sum = App_21::sum($owner_cards);
		
		$guest_cards_str = App_21::num2card($guest_cards);
		$guest_cards_sum = App_21::sum($guest_cards);
		
		$smarty->assign('owner_cards' , $owner_cards);
		$smarty->assign('guest_cards' , $guest_cards);
		
		$smarty->assign('owner_cards_str' , $owner_cards_str);
		$smarty->assign('owner_cards_sum' , $owner_cards_sum);
		
		$smarty->assign('guest_cards_str' , $guest_cards_str);
		$smarty->assign('guest_cards_sum' , $guest_cards_sum);
		
		
		$smarty->assign('is_own' , $is_own);
		$smarty->assign('owner_uin' , $owner_uin);
		$smarty->assign('id' , $id);
		
		$is_owner_win = $owner_cards_sum >= $guest_cards_sum ? true : false;
		
		if($is_owner_win) {
			// 主人赢
			App_21::lose($owner_uin , $id , $state['guest_uin'] , $state);
			$smarty->assign('win_user' , 'owner');
		} else {
			// 客人赢
			App_21::lose($owner_uin , $id , $state['uin'] , $state);
			$smarty->assign('win_user' , 'guest');
		}
		
		// 给主人发系统消息
		loadLib('SysMsg');
		SysMsg::sendSystem($owner_uin , array(
			'sum' => App_21::sum($owner_cards),
			'send_sum' => App_21::sum($guest_cards),
			'send_uin' => $uin,
			'win' => $is_owner_win ? 1 : 0,
			'earn' => $stake,
		) , 1001);
		
		
		$smarty->assign('stake' , $stake);
		return $smarty->fetch('app/21/result.tpl.php');
	}
	
	/**
	 * 不加注了，修改turn
	 */
	function endturn() {
		$owner_uin = isset($_POST['uin']) ? intval($_POST['uin']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
		
		$goto_url = SITEURL . '/app.php?m=21&a=openlist&sid=' . sessionInfo('skey');
		
		// 异常判断
		if(empty($owner_uin) || $id <= 0) {
			return View::error('系统繁忙，请稍后再试');
			
		}
		
		// 获取是否存在该局
		loadLib('App_21');
		$state = App_21::getState($owner_uin , $id);
		
		if(empty($state)) {
			header('Location: ' . $goto_url);
			return;
		}
		
		$uin = sessionInfo('uin');
		$is_own = $uin == $owner_uin ? true : false;
		
		// 只有主人才能修改turn
		if(!$is_own) {
			return View::error('系统繁忙，请稍后再试');
		}
		
		// 记录当前牌局(修改turn)
		$code = App_21::recordState($owner_uin , $id , $state['guest_uin'] , 1 , $state['state']['owner_cards'] , $state['state']['guest_cards'] , $state['stake']);
		
		// 不管是否成功(后面有定时脚本，定期修复turn)
		header('Location: ' . $goto_url);
		return;
	}
	
	/**
	 * 加注
	 */
	function addstake() {
		$owner_uin = isset($_POST['uin']) ? intval($_POST['uin']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
		$add_stake = isset($_POST['add_stake']) ? intval($_POST['add_stake']) : 0;
		
		// 异常判断
		if(empty($owner_uin) || $id <= 0) {
			return View::error('系统繁忙，请稍后再试');
		}
		
		if($add_stake <= 0) {
			header('Location: ' . SITEURL . '/app.php?m=21&a=openlist&sid=' . sessionInfo('skey') );
		}
		
		$uin = sessionInfo('uin');
		
		$is_own = $uin == $owner_uin ? true : false;
		
		// 只有主人才能加注
		if(!$is_own) {
			return View::error('系统繁忙，请稍后再试');
		}
		
		// 获取是否存在该局
		loadLib('App_21');
		$state = App_21::getState($uin , $id);
		
		if(empty($state)) {
			return View::error('该局已结束');
		}
		
		$smarty = loadSmarty();
		
		// 判断用户是否有这么多钱加注
		loadLib('Currency');
		$money = Currency::get('app_21' , $uin);
		
		$smarty->assign('money' , $money);
		
		// 判断钱是否够，不够就提示去用筹码换，或者退出
		if($money < $add_stake) {
			return View::error('你的筹码不够 ' . $add_stake . '，不能加注');
		}
		
		// 判断当前状态，主人是否还有权限继续加注
		// ..todo
		
		
		// 开始扣钱，加注
		$owner_cards = $state['state']['owner_cards'];
		$guest_cards = $state['state']['guest_cards'];
		$stake = $state['stake'];
		
		// 记录当前牌局(修改赌注)
		$code = App_21::recordState($owner_uin , $id , $state['guest_uin'] , $state['turn'] , $owner_cards , $guest_cards , $stake + $add_stake);
		if($code !== 0) {
			return View::error('系统繁忙，请稍后再试' , $code);
		}
		
		
		// 最后一步，扣钱 (就算这步失败了，也是用户赚点，用户不会亏，不会投诉)
		$code = Currency::update('app_21' , $uin , 0 - $add_stake);
		if($code !== 0) {
			return View::error('系统繁忙，请稍后再试' , $code);
		}
		
		return View::success('加注 ' . $add_stake . ' 筹码成功');
	}
	
	/**
	 * 应战
	 */
	function challenge() {
		$owner_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		
		// 异常判断
		if(empty($owner_uin) || $id <= 0) {
			return View::error('系统繁忙，请稍后再试');
		}
		
		$uin = sessionInfo('uin');
		
		$is_own = $uin == $owner_uin ? true : false;
		
		// 只有主人才能加注
		if($is_own) {
			return View::error('不能应战自己的赌局');
		}
		
		// 获取是否存在该局
		loadLib('App_21');
		$state = App_21::getState($owner_uin , $id);
		
		if(empty($state)) {
			return View::error('你下手太慢拉，别人已经应战了');
		}
		
		$is_own = $uin == $owner_uin ? true : false;
		
		// 判断是否有权限继续摸牌
		if($state['guest_uin'] > 0 || $state['turn'] != 1) {
			return View::error('你下手太慢拉，别人已经应战了');
		}
		
		$smarty = loadSmarty();
		
		$owner_cards = $state['state']['owner_cards'];
		$guest_cards = $state['state']['guest_cards'];
		$stake = $state['stake'];
		
		// 判断用户是否够钱
		loadLib('Currency');
		$money = Currency::get('app_21' , $uin);
		
		$smarty->assign('money' , $money);
		
		// 判断钱是否够，不够就提示去用筹码换，或者退出
		if($money < $stake) {
			return View::error('你的筹码不够 ' . $stake . '，不能应战');
		}
		
		// 随机抽2张牌
		$guest_cards = App_21::deal(2);
		$cards_str = App_21::num2card($guest_cards);
		$cards_sum = App_21::sum($guest_cards);
		
		$smarty->assign('cards' , $guest_cards);
		$smarty->assign('cards_str' , $cards_str);
		$smarty->assign('cards_sum' , $cards_sum);
		
		// 牌是好的，则记录当前状态
		$code = App_21::recordState($owner_uin , $id , $uin , 2 , $owner_cards , $guest_cards , $stake);
		if($code !== 0) {
			return View::error('系统繁忙，请稍后再试' , $code);
		}
		
		
		// 最后一步，扣钱 (就算这步失败了，也是用户赚点，用户不会亏，不会投诉)
		$code = Currency::update('app_21' , $uin , 0 - $stake);
		if($code !== 0) {
			return View::error('系统繁忙，请稍后再试' , $code);
		}
		loadLib('Operate');
		Operate::getAdd($uin , $owner_uin , 'app_21_challenge');
		// 全部成功后，302 跳转到再摸一张的页面，防止用户刷新页面，再次开一局
		header('Location: ' . SITEURL . '/app.php?m=21&a=addcard&uin=' . $owner_uin . '&id=' . $id . '&sid=' . sessionInfo('skey') );
	}
	
	/**
	 * 查看用户已开的赌局
	 */
	function openlist() {
		$uin = sessionInfo('uin');
		
		// 获取是否存在该局
		loadLib('App_21');
		$offset = 0;
		$result = App_21::getUserStateList($uin , $offset);
		
		$list = array();
		foreach($result as $k => $row) {
			$list[$k + $offset] = $row;
			$list[$k + $offset]['cards_str'] = App_21::num2card($row['state']['owner_cards']);
			$list[$k + $offset]['cards_sum'] = App_21::sum($row['state']['owner_cards']);
		}
		//print_r($list);
		$smarty = loadSmarty();
		$smarty->assign('list' , $list);
		//print_r($list);
		return $smarty->fetch('app/21/openlist.tpl.php');
	}
	
	
	
	/**
	 * 赢钱排行榜
	 */
	function top() {
		$smarty = loadSmarty();
		
		$top_type = isset($_GET['type']) && in_array($_GET['type'] , array('rate' , 'gain')) ? trim($_GET['type']) : 'gain';
		
		// 获取排行
		loadLib('App_21');
		$list = App_21::getTopList( $top_type , 0 , 20 );
		
		$uin_arr = array();
		$gains = array();
		foreach($list as $row) {
			$uin_arr[] = $row['uin'];
			$gains[$row['uin']] = $row['gain'];
		}
		
		// 获取用户等级信息
		$gains_info = App_21::getRanks($uin_arr , $gains);
		
		loadLib('User');
		$uin_arr_info = User::getBaseInfo($uin_arr);
		foreach($list as $k => $row) {
			$list_uin = $row['uin'];
			$list[$k]['name'] = isset($uin_arr_info[$list_uin]) ? $uin_arr_info[$list_uin]['name'] : '';
			$list[$k]['sex'] = isset($uin_arr_info[$list_uin]) ? $uin_arr_info[$list_uin]['sex'] : '';
			$list[$k]['rank'] = $gains_info[$list_uin];
			$list[$k]['rate'] = sprintf('%.2f' , $row['rate'] / 100);
		}
		
		$smarty->assign('list' , $list);
		$smarty->assign('top_type' , $top_type);
		
		return $smarty->fetch('app/21/top.tpl.php');
	}
	
	/**
	 * 刷新过期没有处理的turn(用户没按流程走完)
	 */
	function refreshturn() {
		$time = isset($_GET['time']) ? intval($_GET['time']) : 86400;
		
		$expire_time = time() - $time;
		
		$Db = loadDB();
		$result = $Db->getAll("SELECT `uin` , `id` FROM `app_21_state` WHERE `turn` = 0 AND `time` < {$expire_time}");
		
		if(!empty($result)) {
			$sql_arr = array();
			foreach($result as $row) {
				$Db->query("UPDATE `app_21_state` SET `turn` = 1 WHERE `uin` = {$row['uin']} AND `id` = {$row['id']}");
			}
		}
		
		/*
		$result = $Db->getAll("SELECT `uin` , `id` FROM `app_21_state` WHERE `turn` = 2 AND `time` < {$expire_time}");
		
		if(!empty($result)) {
			$sql_arr = array();
			foreach($result as $row) {
				$Db->query("UPDATE `app_21_state` SET `turn` = 1 WHERE `uin` = {$row['uin']} AND `id` = {$row['id']}");
			}
		}
		*/
		
		echo 'ok';
	}
	
	/**
	 * 21点玩法
	 */
	function morastate(){
		$smarty = loadSmarty();
		
		return $smarty->fetch('app/21/state.tpl.php');
	}
	/**
	 * 删除自身的牌局,扣除当前牌局的10%筹码
	 */
	function delete_21(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
		$stake = isset($_POST['stake']) ? intval($_POST['stake']) : 0;
		if(empty($id)){
			return View::error('参数错误!');
		}
		$uin = SessionInfo('uin');
		$stake = $stake*0.9;
		loadLib('App_21');
		$code = App_21::delState($uin, $id);
		if($code === 0){
			loadLib('Currency');
			Currency::update('app_21' , $uin , +$stake);			
			header('Location: ' . SITEURL . '/app.php?m=21&sid=' . sessionInfo('skey') );
		}else{
			return View::error('系统繁忙，请稍后再试' , $code);
		}
	}
	/**
	 * 21点道具
	 */
	function prop(){
		$smarty = loadSmarty();
		loadLib('App_21');
		$uin = sessionInfo('uin');
		// 获取自己的战绩
		$gains = App_21::getGain($uin , true);
		//获取梭哈道具
		loadLib('App_prop');
		$prop = App_prop::getAllProp('App_21_prop');
		
		$smarty->assign('list' , $prop);
		$oneselfprop = App_prop::getAllOneselfProp('App_21_oneself_prop', 'App_21_prop', $uin);
	//print_r($prop);
		// 获取盈利
		$rank = App_21::getRank($uin , $gains['gain']);
		/*******************查询金库******************/
		loadLib('Money');
		$money = Money::get($uin);
		if (empty($money)){
			return View::error('你的金库不存在!!!');
		}
		
		$smarty->assign('oneselfprop' , $oneselfprop);
		$smarty->assign('money',$money);
		$smarty->assign('rank' , $rank['name']);
		
		return $smarty->fetch('app/21/prop.tpl.php');
	}
	/**
	 * 购买道具
	 */
	function addProp(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$photoscope = isset($_POST['photoscope']) ? intval($_POST['photoscope']) : null;
		if (empty($id) || empty($photoscope)){
			return View::error('参数错误!!!');
		}
		
		loadLib('App_prop');
		$list = App_prop::getOneProp('App_21_prop', $id);
		if (empty($list)){
			return View::error('该道具不存在!!!');
		}
		$uin=sessionInfo('uin');
		/*******************查询金库******************/
		loadLib('Money');
		$money = Money::get($uin);
		if (empty($money)){
			return View::error('你的金库不存在!!!');
		}
		$gold = $photoscope * $list['price'];
		
		if ($gold > $money['gold']){
			return View::error('你的金币不足'.$gold.',请及时充值!!!');
		}
		/**********************查询自己的道具************************/
		$prop = App_prop::getOneselfProp('App_21_oneself_prop', $uin, $id);
		/**********************记录为空时写入************************/
		if (empty($prop)){
			App_prop::getAddOneselfProp('App_21_oneself_prop', $uin, $id, $photoscope);
		}else{
			//$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
			$count = $prop['count'] + $photoscope;
			App_prop::getAlterOneselfProp('App_21_oneself_prop', $id, $uin, $count, $prop['time']);
		}
		/***********************扣除金币********************/
		$code = Money::update($uin, -$gold);
		if($code === 0){
			$money = $money['gold'] - $gold;
			//消费记录
			loadLib('Operate');
			Operate::getAddConsume($uin, 'app_21_prop', $id, $gold);
			return View::success('你购买了' . $photoscope .'个'.$list['name'].',你的余额为' . $money . '金币');
		}else{
			return View::error('购买失败');
		}
	}
	/**
	 * 使用道具
	 */
	function scene(){
		$owner_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$prop_id = isset($_GET['prop']) ? intval($_GET['prop']) : null;
		$uin = sessionInfo('uin');
		$smarty = loadSmarty();
		
		loadLib('App_21');
		
		$state = App_21::getState($owner_uin , $id);
		if (empty($state)){
			return View::error('该局不存在!!!');
		}
		
		loadLib('App_prop');
		$list = App_prop::getOneProp('App_21_prop', $prop_id);
		if (empty($list)){
			return View::error('该道具不存在!!!');
		}
		
		/**********************查询自己的道具************************/
		$prop = App_prop::getOneselfProp('App_21_oneself_prop', $uin, $prop_id);
		if (empty($prop)){
			return View::error('你没有此道具!!!' , 0 ,array(
				array('name' => '前往商店' , 'url' => '/app.php?m=showhand&amp;a=showhandproperty&amp;sid=' . sessionInfo('skey')),
				));
		}
		// 随机发两张牌
		$cards = App_21::deal(2);
		if (empty($state['state']['guest_cards'])){
			$state['state']['owner_cards'] = '';
			$state['state']['owner_cards'] = $cards;
			$code=App_21::getUpstate($owner_uin , $id , $state['state']);
		}else{
			$state['state']['guest_cards'] = '';
			$state['state']['guest_cards'] = $cards;
			$code=App_21::getUpstate($owner_uin , $id , $state['state']);
		}
		/*****************重开*****************/
		if ($list['type'] == 'repeat') {
			if (empty($state['state']['guest_cards'])){
				//防止刷新
				header('Location: ' . SITEURL . '/app.php?m=21&a=addcard&uin=' . sessionInfo('uin') . '&id=' . $id . '&sid=' . sessionInfo('skey') );	
			}else{
				header('Location: ' . SITEURL . '/app.php?m=21&a=addcard&uin=' . $owner_uin . '&id=' . $id . '&sid=' . sessionInfo('skey') );	
			}
		}
		if ($code == 0){
			$count = $prop['count'] -1;
			if ($count <= 0){
				App_prop::delProp('App_21_oneself_prop', $uin, $prop_id);
			}else{
				App_prop::getAlterOneselfProp('App_21_oneself_prop', $prop_id, $uin, $count, $prop['time']);
			}
		}else{
				return View::error('网络繁忙,请稍后再试！');
		}
		
	}
}


# end of script
